#!/bin/bash -eu
#
# Copyright 2018 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

source /opt/c2d/c2d-utils || exit 1

set +e
CLUSTER="$(get_attribute_value cluster)"
CEPHNODES="$(get_attribute_value cephnodes)"
DATADISKS="$(get_attribute_value number-of-data-disks)"
CONFIGNAME="$(get_attribute_value ceph-config-name)"
set -e
DEPUSER=<%= node['ceph']['deploymentuser'] %>
DEPUSERHOME="/home/${DEPUSER}"
AUTHORIZEDKEYS=<%= node['ceph']['rsync-dir'] %>/authorized_keys

su - "${DEPUSER}" -c "ssh-keygen -t rsa -b 2048 -C '${DEPUSER}@ceph-adm' -f ${DEPUSERHOME}/.ssh/id_rsa -N ''"
cp "${DEPUSERHOME}"/.ssh/id_rsa.pub "${AUTHORIZEDKEYS}"
chown "${DEPUSER}":"${DEPUSER}" "${AUTHORIZEDKEYS}"
chmod 644 "${AUTHORIZEDKEYS}"

for NODE in ${CEPHNODES}; do
  ssh-keyscan -T3 -H "${NODE}" > "${DEPUSERHOME}"/.ssh/known_hosts-tmp
  while [[ ! -s "${DEPUSERHOME}"/.ssh/known_hosts-tmp ]]; do
    sleep 3
    ssh-keyscan -T3 -H "${NODE}" > "${DEPUSERHOME}"/.ssh/known_hosts-tmp
  done
  cat "${DEPUSERHOME}"/.ssh/known_hosts-tmp >> "${DEPUSERHOME}"/.ssh/known_hosts
  rm "${DEPUSERHOME}"/.ssh/known_hosts-tmp
  cat <<EOF >>"${DEPUSERHOME}"/.ssh/config
Host ${NODE}
  Hostname ${NODE}
  User ${DEPUSER}
EOF
done

for NODE in ${CEPHNODES}; do
  while ! su - "${DEPUSER}" -c "ssh ${NODE} exit"; do sleep 3; done
done

su - "${DEPUSER}" -c "mkdir ${CLUSTER}"
su - "${DEPUSER}" -c "cd ${CLUSTER}; ceph-deploy new ${CEPHNODES}"
su - "${DEPUSER}" -c "cd ${CLUSTER}; ceph-deploy mon create-initial"
su - "${DEPUSER}" -c "cd ${CLUSTER}; ceph-deploy admin ${CEPHNODES}"

su - "${DEPUSER}" -c "cd ${CLUSTER}; ceph-deploy mds create ${CEPHNODES}"
su - "${DEPUSER}" -c "cd ${CLUSTER}; ceph-deploy mgr create ${CEPHNODES}"

PASSWORD="$(awk '/key = / {print $3}' /home/cephdep/"${CLUSTER}"/ceph.client.admin.keyring 2>/dev/null)"
if [[ -n "${CONFIGNAME}" ]]; then
  echo 'Setting runtime variables.'
  set_rtc_var_text "${CONFIGNAME}" defaultPassword "${PASSWORD}"
fi

## this operation can take a lot of time
{
for DISK in $(seq 0 $((DATADISKS-1))); do
  for NODE in ${CEPHNODES}; do
    su - "${DEPUSER}" -c "cd ${CLUSTER}; ceph-deploy osd create ${NODE} --data /dev/disk/by-id/google-${NODE}-disk-data-${DISK}"
  done
done
set_rtc_var_text "${CONFIGNAME}" disks-configuration/status success
} &

